/**
  ******************************************************************************
  * @file    FreeRTOSHook.c
  * @author  Iron
  * @date    2018-07-09
  * @version v1.0
  * @brief   template c file
  */

/** @addtogroup GROUP_FreeRTOS
  * @{
  */

/* includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"

// DEBUG
#include "debug_log.h"
#define LOG_LOCAL_LEVEL DBG_LOG_DEBUG
DBG_LOG_TAG("FREERTOS_HOOK");

/* private typedef -----------------------------------------------------------*/
/* private macro -------------------------------------------------------------*/
/* private variables ---------------------------------------------------------*/
/* private function prototypes -----------------------------------------------*/
/* private functions ---------------------------------------------------------*/

void vApplicationIdleHook(void)
{
}

void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName)
{
    DBG_LOGE(TAG, "Fatal error, Task %s Stack Overflow!", pcTaskName);
    while (1) {}
}

void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize)
{
    static StaticTask_t xIdleTaskTCB;
    static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];

    *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
    *ppxIdleTaskStackBuffer = uxIdleTaskStack;
    *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
}

void vApplicationGetTimerTaskMemory(StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize)
{
    static StaticTask_t xTimerTaskTCB;
    static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];

    *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;
    *ppxTimerTaskStackBuffer = uxTimerTaskStack;
    *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
}

#if configGENERATE_RUN_TIME_STATS
#include "stm32f4xx_hal.h"
extern TIM_HandleTypeDef htim1; //
static uint32_t rtos_start_ticks;

static uint32_t get_run_time_ticks(void)
{
    //uint32_t us = __HAL_TIM_GET_COUNTER(&htim1);
    //uint32_t ms = HAL_GetTick();
    //return (us + ms * 1000);  // us

    return HAL_GetTick(); // ms
}

void port_config_timer_for_run_time_stats(void)
{
    rtos_start_ticks = get_run_time_ticks();
}

uint32_t port_get_run_time_counter_value(void)
{
    return (get_run_time_ticks() - rtos_start_ticks);
}
#endif

/**
  * @}
  */

/******************* (C)COPYRIGHT 2018 ***** END OF FILE *********************/
